package com.qf.leadnewswemedia.service.impl;

import com.qf.leadnewswemedia.service.WmNewsService;
import com.qf.qfleadnewsfeignapi.schedule.ScheduleApi;
import com.qf.qfleadnewsmodel.enums.TaskTypeEnum;
import com.qf.qfleadnewsmodel.schedule.dtos.Task;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmNews;
import com.qf.qfleadnewsutils.commons.ProtostuffUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * 发布文章任务的定时消费业务
 */
@Service
@Slf4j
public class PublishNewsTimerService {

    @Autowired
    private ScheduleApi scheduleApi;

    @Autowired
    private WmNewsService wmNewsService;

    @Scheduled(cron = "0/1 * * * * ?")
    public void pubNews(){
        Task task = scheduleApi.consumeTask(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType(), TaskTypeEnum.NEWS_SCAN_TIME.getPriority());

        if (task != null){
            //文章id序列化后的内容
            byte[] parameters = task.getParameters();
            //将其反序列化为文章id
            String wmnewsIdStr = ProtostuffUtil.deserialize(parameters, String.class);
            log.info("--审核&发布文章....,文章id为：{}--",wmnewsIdStr);

            WmNews wmNews = wmNewsService.getById(Integer.parseInt(wmnewsIdStr));

            try {
                wmNewsService.scanNews(wmNews);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
}
